package com.ruoyi.petSys.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.petSys.entity.vo.ChatMsgVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.petSys.mapper.ChatMsgMapper;
import com.ruoyi.petSys.domain.ChatMsg;
import com.ruoyi.petSys.service.IChatMsgService;

/**
 * 聊天记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-03
 */
@Service
public class ChatMsgServiceImpl implements IChatMsgService 
{
    @Autowired
    private ChatMsgMapper chatMsgMapper;

    /**
     * 查询聊天记录
     * 
     * @param id 聊天记录主键
     * @return 聊天记录
     */
    @Override
    public ChatMsgVo selectChatMsgById(Long id)
    {
        return chatMsgMapper.selectChatMsgById(id);
    }

    /**
     * 查询聊天记录列表
     * 
     * @param chatMsg 聊天记录
     * @return 聊天记录
     */
    @Override
    public List<ChatMsgVo> selectChatMsgList(ChatMsg chatMsg)
    {
        List<ChatMsgVo> chatMsgVos  = new ArrayList<>();
        if(ObjectUtil.isEmpty(chatMsg.getSendUserId()) || ObjectUtil.isEmpty(chatMsg.getReceiveUserId())){
            return  chatMsgVos;
        }
        chatMsgVos = chatMsgMapper.selectChatMsgList(chatMsg);
        chatMsgVos = chatMsgVos.stream().peek(chatMsgVo -> {
            if("/dev-api".equals(chatMsgVo.getSendAvatar())){
                chatMsgVo.setSendAvatar("/icon/profile.jpg");
            }
            if("/dev-api".equals(chatMsgVo.getReceiveAvatar())){
                chatMsgVo.setReceiveAvatar("/icon/profile.jpg");
            }
        }).collect(Collectors.toList());
        return chatMsgVos;
    }

    /**
     * 新增聊天记录
     * 
     * @param chatMsg 聊天记录
     * @return 结果
     */
    @Override
    public int insertChatMsg(ChatMsg chatMsg)
    {
        chatMsg.setCreateTime(DateUtils.getNowDate());
        return chatMsgMapper.insertChatMsg(chatMsg);
    }

    /**
     * 修改聊天记录
     * 
     * @param chatMsg 聊天记录
     * @return 结果
     */
    @Override
    public int updateChatMsg(ChatMsg chatMsg)
    {
        chatMsg.setUpdateTime(DateUtils.getNowDate());
        return chatMsgMapper.updateChatMsg(chatMsg);
    }

    /**
     * 批量删除聊天记录
     * 
     * @param ids 需要删除的聊天记录主键
     * @return 结果
     */
    @Override
    public int deleteChatMsgByIds(Long[] ids)
    {
        return chatMsgMapper.deleteChatMsgByIds(ids);
    }

    /**
     * 删除聊天记录信息
     * 
     * @param id 聊天记录主键
     * @return 结果
     */
    @Override
    public int deleteChatMsgById(Long id)
    {
        return chatMsgMapper.deleteChatMsgById(id);
    }
}
